package com.minyifang.wcspf.work.ui.user.activities;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.base.BaseMvpActivity;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.common.utils.ToastHelper;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.user.model.MessageListBean;
import com.minyifang.wcspf.work.ui.user.presenter.MessageListPresenter;
import com.minyifang.wcspf.work.ui.user.view.MessageListView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MessageListActivity extends BaseMvpActivity<MessageListView, MessageListPresenter> implements MessageListView {

    public static final String INTENT_TYPE = "TYPE";

    @BindView(R.id.swipeRefreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private String type;

    private List<MessageListBean> mList;
    private MessageListAdapter mAdapter;

    public static void start(Context context, String type) {
        Intent intent = new Intent(context, MessageListActivity.class);
        intent.putExtra(INTENT_TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_message_list;
    }

    @Override
    protected void initView() {

        Intent intent = getIntent();
        if (intent != null) {
            type = intent.getStringExtra(INTENT_TYPE);
        }

        mList = new ArrayList<>();
        mAdapter = new MessageListAdapter(R.layout.item_msg, mList);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(mAdapter);

    }

    @Override
    protected void initData() {
        mRefreshLayout.setOnRefreshListener(refreshLayout -> mPresenter.getMessageList(type));
        mRefreshLayout.autoRefresh();
    }

    @Override
    protected MessageListPresenter createPresenter() {
        return new MessageListPresenter();
    }

    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {

    }

    @Override
    public void onNetworkError() {
        mRefreshLayout.finishRefresh();
    }

    @Override
    public void showMessageList(HttpRespond<String> respond) {
        mRefreshLayout.finishRefresh();
        if (respond.result == 1) {
            if (respond.isaes == 1) {
                LogUtil.e(SafeUtils.decrypt(respond.data));
                List<MessageListBean> list = new Gson().fromJson(SafeUtils.decrypt(respond.data), new TypeToken<List<MessageListBean>>() {
                }.getType());
                mList.clear();
                mList.addAll(list);
                mAdapter.notifyDataSetChanged();
            }
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);

    }


    class MessageListAdapter extends BaseQuickAdapter<MessageListBean, BaseViewHolder> {

        public MessageListAdapter(int layoutResId, @Nullable List<MessageListBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, MessageListBean item) {
            helper.setText(R.id.tv_time, item.getSend_time());
            helper.setText(R.id.tv_title, item.getTitle());
            helper.setText(R.id.tv_content, item.getContent());
        }
    }
}
